package npr.gui;
import processing.core.*;


public class RadioButton extends GUI{


    private String label;
    private float labelPos;
    private String[] labels;
    private Circle[] circles;
    private int radius= 5;
    private int checked;


    public RadioButton(int xpos, int ypos, String label, String[] labels, int checked){
	this(xpos, ypos, null, defaultFontSize, label, labels, checked);
    }


    public RadioButton(int xpos, int ypos, PFont font, int fontSize, String label, String[] labels, int checked){
	super(xpos, ypos, 0, 0, font, fontSize);
	height= 2*radius+1;
	this.label= label;
	labelPos= labelXPos(label);
	this.labels= labels;
	circles= new Circle[labels.length];
	for(int i= 0; i<circles.length; i++){
	    width+=radius;
	    circles[i]= new Circle(xpos+width, (int)(ypos+height/2.0f), radius, checked==i);
	    width+=radius+fontSize;
	    width+= labelXPos(labels[i]);
	}
	this.checked= checked;
    }

    public void draw(){

	if(display){       
	    parent.hint(DISABLE_DEPTH_TEST);
	    parent.textFont(font, fontSize);
	    parent.fill(fontColor);
	    parent.text(label, xpos-labelPos, ypos+height/2+font.ascent()*fontSize*0.5f);
	    for(int i= 0; i<circles.length; i++){
		circles[i].draw();
		parent.fill(fontColor);
		parent.text(labels[i], circles[i].xpos+fontSize, ypos+height/2+font.ascent()*fontSize*0.5f);
	    }
	    parent.hint(ENABLE_DEPTH_TEST);
	}
    }

    protected void mousePressed(int x, int y){

	for(int i= 0; i<circles.length; i++){
	    if(circles[i].inside(x, y)){
		circles[checked].checked= false;
		checked= i;
		circles[checked].checked= true;
	    }
	}
    }


    public int checked(){
	return checked;
    }


    class Circle{

	float k= 2.0f*0.5522847498f;
	int xpos, ypos;
	int radius;
	boolean checked;

	Circle(int xpos, int ypos, int radius, boolean checked){
	    this.xpos= xpos;
	    this.ypos= ypos;
	    this.radius= radius;
	    this.checked= checked;
	}
	
	boolean inside(int x, int y){
	    return(parent.sqrt(parent.sq(x-xpos)+parent.sq(y-ypos)))<radius;
	}

	void draw(){
	    parent.smooth();
	    parent.pushMatrix();
	    parent.translate(xpos, ypos);
	    //parent.rotate(PI/4);
       	    parent.ellipseMode(CENTER);
	    
	    
	    parent.noFill();
	    if(enabled) parent.stroke(180);
	    else parent.stroke(200);
	    parent.ellipse(0, 0, 2*radius, 2*radius); 
	    if(enabled) parent.stroke(230);
	    else parent.stroke(240);
	    parent.ellipse(0, 0, 2*radius+2, 2*radius+2);
	    if(enabled) parent.stroke(0);
	    else parent.stroke(100);
	    float pos=(float)Math.sin(PI/4.0f)*(float)(radius);

	    parent.bezier(-pos, pos, 0.94f*(-pos-(float)radius), 0.94f*(pos-(float)radius), 0.94f*(pos-(float)radius), 0.94f*(-pos-(float)radius), pos, -pos);

parent.stroke(255);
	    parent.fill(255);
	    parent.ellipse(0, 0, 2*radius-2, 2*radius-2);


	    if(checked){
		parent.fill(fontColor);
		parent.stroke(fontColor);
		parent.ellipse(0, 0, 2*radius-4, 2*radius-4);
		parent.stroke(255);
		parent.noFill();
		parent.ellipse(0, 0, 2*radius-2, 2*radius-2);

	    }
	    
	    parent.noSmooth();
	    parent.popMatrix();
	}
    }
}

